/*
 * ValidateCode.java
 *
 * Copyright 2013 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.core.util, Version 1.0 (the "License");
 *
 */
package org.geek.core.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
public class ValidateCode {
	public static String [] fontNames =null;
	public static int [] fontType = {Font.PLAIN,Font.ITALIC,Font.BOLD};
	public static Random random = new Random();
	
	static{
		GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();//系统字体
		fontNames = gEnv.getAvailableFontFamilyNames();//所有系统字体名称
	}
	
	private int width =160;//图片宽度
	private int height = 40;
	private int codeCount = 4;//验证码长度
	private int lineCount =3;//干扰线数量
	
	private char [] codeSequence = {'A','B','C','1'};//可以自行在这里添加，包括中文、字母、数字
	
	private String code = null;
	private byte []datas =null;
	
	public static ValidateCode getValidateCode(){
		ValidateCode vc = new ValidateCode();
		vc.createCode();
		return vc;
	}
	private Font getFont(int fontHeight){
		return Font.decode(fontNames[random.nextInt(fontNames.length)]).deriveFont(fontType[random.nextInt(fontType.length)],fontHeight);
	}
	private void createCode(){
		int x = 0;
		int fontHeight = 0;
		int codeY = 0;
		int red =0 ;
		int green=0;
		int blue = 0;
		x = this.width/(this.codeCount +2);
		fontHeight = this.height-4;
		codeY = this.height-2;
		BufferedImage buffImg = new BufferedImage(width, height, 1);
		Graphics2D g = buffImg.createGraphics();
		
		Random random = new Random();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);
		for(int i=0;i<this.lineCount;i++){
			int xs = random.nextInt(this.width);
			int ys = random.nextInt(this.height);
			int xe = xs + random.nextInt(this.width/8);
			int ye = ys + random.nextInt(this.height/8);
			red = random.nextInt(120);
			green = random.nextInt(120);
			blue = random.nextInt(120);
			g.setColor(new Color(red,green,blue));
			g.drawLine(xs, ys, xe, ye);
		}
		StringBuffer randomCode = new StringBuffer();
		double sumR = 0.0d;//旋转值
		Font font = getFont(fontHeight);//随机字体
		for(int i=0;i<this.codeCount;i++){
			char c = codeSequence[random.nextInt(this.codeSequence.length)];
			while(!font.canDisplay(c)){//此font没有指定字符的字形
				font = getFont(fontHeight);//重新生成一个
			}
			g.setFont(font);
			sumR = -sumR +(random.nextDouble()-0.5d)*Math.PI*0.04;
			g.rotate(sumR);
			red = random.nextInt(120);
			green = random.nextInt(120);
			blue = random.nextInt(120);
			g.setColor(new Color(red,green,blue));
			g.drawString(String.valueOf(c), (i+1)*x,codeY);
			
			randomCode.append(String.valueOf(c));
		}
		g.dispose();
		
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(new File("c:\\s.jpeg"));
			ImageIO.write(buffImg, "JPEG", fos);
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			try {
				if(fos!=null){
					fos.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		this.code = randomCode.toString();
	}
	public String getCode(){
		return this.code;
	}
	public byte[] getDatas(){
		return datas;
	}
}
